/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Login.java
 *
 * Created on Oct 13, 2012, 1:51:23 PM
 */
package thienphuc.patient.screen;

import java.awt.Toolkit;
import javax.swing.JOptionPane;
import thienphuc.patient.bo.NhanVienBO;
import thienphuc.patient.bo.impl.NhanVienBOImpl;
import thienphuc.patient.dao.common.CommonDAO;
import thienphuc.patient.dto.NhanVien;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;

/**
 *
 * @author QuocDat
 */
public class Login extends JFrame {
    
    private NhanVienBO nhanVienBO;

    /** Creates new form Login */
    public Login() {
        initComponents();
        
        this.nhanVienBO = new NhanVienBOImpl();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnLogin = new javax.swing.JPanel();
        lblPassword = new javax.swing.JLabel();
        btnLogin = new javax.swing.JButton();
        txtUsername = new javax.swing.JTextField();
        lblUsername = new javax.swing.JLabel();
        txtPassword = new javax.swing.JPasswordField();
        lblImageStethoscope = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("thienphuc/patient/main/resources/login"); // NOI18N
        setTitle(bundle.getString("thienphuc.patient.main.resources.login.title")); // NOI18N
        setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/thienphuc/patient/images/icons16x16/hospital.png"))   );
        setName("jFrameLogin"); // NOI18N

        pnLogin.setMaximumSize(new java.awt.Dimension(800, 600));
        pnLogin.setName("pnLogin"); // NOI18N

        lblPassword.setText(bundle.getString("thienphuc.patient.main.resources.login.lblPassword")); // NOI18N
        lblPassword.setName("lblPassword"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(thienphuc.patient.main.ThienPhucPatientApp.class).getContext().getResourceMap(Login.class);
        btnLogin.setIcon(resourceMap.getIcon("btnLogin.icon")); // NOI18N
        btnLogin.setText(bundle.getString("thienphuc.patient.main.resources.login.btnLogin")); // NOI18N
        btnLogin.setName("btnLogin"); // NOI18N
        btnLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnLoginActionPerformed(evt);
            }
        });

        txtUsername.setText(resourceMap.getString("txtUsername.text")); // NOI18N
        txtUsername.setName("txtUsername"); // NOI18N
        txtUsername.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtUsernameActionPerformed(evt);
            }
        });
        txtUsername.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txtUsernameFocusGained(evt);
            }
        });

        lblUsername.setText(bundle.getString("thienphuc.patient.main.resources.login.lblUsername")); // NOI18N
        lblUsername.setName("lblUsername"); // NOI18N

        txtPassword.setText(resourceMap.getString("txtPassword.text")); // NOI18N
        txtPassword.setName("txtPassword"); // NOI18N
        txtPassword.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtPasswordActionPerformed(evt);
            }
        });
        txtPassword.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txtPasswordFocusGained(evt);
            }
        });

        lblImageStethoscope.setIcon(new javax.swing.ImageIcon(getClass().getResource("/thienphuc/patient/images/common/stethoscope.png"))); // NOI18N
        lblImageStethoscope.setText(resourceMap.getString("lblImageStethoscope.text")); // NOI18N
        lblImageStethoscope.setName("lblImageStethoscope"); // NOI18N

        javax.swing.GroupLayout pnLoginLayout = new javax.swing.GroupLayout(pnLogin);
        pnLogin.setLayout(pnLoginLayout);
        pnLoginLayout.setHorizontalGroup(
            pnLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnLoginLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(txtPassword, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, pnLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(btnLogin)
                        .addGroup(pnLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(txtUsername, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
                            .addComponent(lblPassword, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblUsername, javax.swing.GroupLayout.Alignment.LEADING))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblImageStethoscope)
                .addContainerGap(0, Short.MAX_VALUE))
        );
        pnLoginLayout.setVerticalGroup(
            pnLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnLoginLayout.createSequentialGroup()
                .addGroup(pnLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblImageStethoscope, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(pnLoginLayout.createSequentialGroup()
                        .addGap(39, 39, 39)
                        .addComponent(lblUsername)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtUsername, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblPassword)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnLogin)))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jBtnLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnLoginActionPerformed
    try {
        String tenTaiKhoan = this.txtUsername.getText();
        String matKhau = new String(this.txtPassword.getPassword());
        //Check input parameter
        if (CommonDAO.checkInput(tenTaiKhoan, matKhau)) {
            //Get NhanVien by TenTaiKhoan and MatKhau
            NhanVien nhanVien = this.nhanVienBO.dangNhap(tenTaiKhoan, matKhau);
            if (nhanVien.getTenTaiKhoan() != null) {
                
                JOptionPane.showMessageDialog(null, "Login success", "Infor", JOptionPane.INFORMATION_MESSAGE);
            } else {
                JOptionPane.showMessageDialog(null, "Tên tài khoản hoặc mật khẩu không đúng, vui lòng nhập lại", "Lỗi", JOptionPane.ERROR_MESSAGE);
                //Set input text is empty
                this.txtUsername.setText("");
                this.txtPassword.setText("");

                //Set forcus for textbox txtUserName
                this.txtUsername.setFocusCycleRoot(true);
            }
        } else {
            //Set input text is empty
            this.txtUsername.setText("");
            this.txtPassword.setText("");

            //Set forcus for textbox txtUserName
            this.txtUsername.setFocusCycleRoot(true);

            //Show message error
            JOptionPane.showMessageDialog(null, "Vui lòng nhập tên tài khoản và mật khẩu", "Lỗi", JOptionPane.ERROR_MESSAGE);
        }

    } catch (Exception ex) {
        Logger.getLogger(Login.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
    }
}//GEN-LAST:event_jBtnLoginActionPerformed

private void txtUsernameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtUsernameActionPerformed
}//GEN-LAST:event_txtUsernameActionPerformed

private void txtUsernameFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtUsernameFocusGained
    this.txtUsername.selectAll();
}//GEN-LAST:event_txtUsernameFocusGained

private void txtPasswordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtPasswordActionPerformed
    this.jBtnLoginActionPerformed(evt);
}//GEN-LAST:event_txtPasswordActionPerformed

private void txtPasswordFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtPasswordFocusGained
    this.txtPassword.selectAll();
}//GEN-LAST:event_txtPasswordFocusGained
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnLogin;
    private javax.swing.JLabel lblImageStethoscope;
    private javax.swing.JLabel lblPassword;
    private javax.swing.JLabel lblUsername;
    private javax.swing.JPanel pnLogin;
    private javax.swing.JPasswordField txtPassword;
    private javax.swing.JTextField txtUsername;
    // End of variables declaration//GEN-END:variables
}
